package org.csu.demo.nettydemo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotBlank;
import org.csu.demo.nettydemo.common.CommonResponse;
import org.csu.demo.nettydemo.domain.User;
import org.csu.demo.nettydemo.persistence.UserMapper;
import org.csu.demo.nettydemo.security.JwtUtils;
import org.csu.demo.nettydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtUtils jwtUtils;

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @GetMapping("/")
    public String login(){
        return "index";
    }

    @PostMapping("/users")
    public String login(@RequestParam("nickname") @NotBlank(message = "用户名不能为空") String nickname,
                              @RequestParam("password") @NotBlank(message = "密码不能为空") String password,
                              HttpSession session,
                              RedirectAttributes redirectAttributes) {
        CommonResponse<User> result = userService.login(nickname, password);

        if (result.isSuccess()) {
            User user = result.getData();
            user.setPassword(null); // 确保密码为 null
            session.setAttribute("loginUser", user);

            //新增
            String token = jwtUtils.generateToken(user.nickname);
            session.setAttribute("token", token);

            return "main";
        } else {
            redirectAttributes.addFlashAttribute("error", "用户名或密码错误");
            return "index";
        }

    }


    // 检查 ID 是否存在的接口
    @GetMapping("/checkId")
    @ResponseBody
    public Map<String, Boolean> checkId(@RequestParam("id") String id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        User user = userMapper.selectOne(queryWrapper);
        Map<String, Boolean> result = new HashMap<>();
        result.put("exists", user != null);
        return result;
    }
    // 用户注册
    @PostMapping("/register")
    public String register(@RequestParam("nickname") String nickname,
                           @RequestParam("password") String password,
                           @RequestParam("id") String id,
                           @RequestParam("confirmPassword") String confirmPassword,
                           RedirectAttributes redirectAttributes) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        User user = userMapper.selectOne(queryWrapper);

        if (user != null) {
            redirectAttributes.addFlashAttribute("error", "用户已存在");
            return "register";
        }
        if (!password.equals(confirmPassword)) {
            redirectAttributes.addFlashAttribute("error", "请输入相同密码");
            return "register";
        }
        user = new User(id, nickname, password);
        userMapper.insert(user);
        redirectAttributes.addFlashAttribute("success", "注册成功");
        return "index";
    }

}
